package cn.neusoft.controller;

import cn.neusoft.domain.Warehouse;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.query.WarehouseQuery;
import cn.neusoft.service.WarehouseService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/warehouse", produces = "application/json;charset=utf-8")
@Api(value = "库房的api", description = "库房相关的CRUD功能")
public class WarehouseController {

    @Autowired
    WarehouseService warehouseService;

    @PostMapping
    @ApiOperation(value = "新增库房")
    public ResultBean add(@RequestBody Warehouse warehouse) {

        if (warehouse == null
                || StringUtils.isEmpty(warehouse.getWarehouse_name())
                || warehouse.getWarn_value() == null || warehouse.getMax_value() == null
                || warehouse.getWarehouse_level() == null
                || StringUtils.isEmpty(warehouse.getStatus())
        ) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        warehouseService.add(warehouse);

        return ResultBean.success();
    }

    @PutMapping
    @ApiOperation(value = "修改库房信息")
    public ResultBean update(@RequestBody Warehouse warehouse) {

        if (warehouse == null
                || warehouse.getWarehouse_id() == null || warehouse.getWarehouse_id() == 0
                || StringUtils.isEmpty(warehouse.getWarehouse_name())
                || warehouse.getWarn_value() == null || warehouse.getMax_value() == null
                || warehouse.getWarehouse_level() == null
                || StringUtils.isEmpty(warehouse.getStatus())
        )  {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        warehouseService.update(warehouse);

        return ResultBean.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除库房")
    public ResultBean deleteById(@PathVariable("id") Long warehouse_id) {

        if (warehouse_id == null) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        warehouseService.deleteById(warehouse_id);

        return ResultBean.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询库房信息")
    public ResultBean findById(@PathVariable("id") Long warehouse_id) {

        if (warehouse_id == null) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Warehouse warehouse = warehouseService.findById(warehouse_id);

        return ResultBean.success(warehouse);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有库房的信息")
    public ResultBean findAll() {
        List<Warehouse> warehouseList = warehouseService.findAll();

        return ResultBean.success(warehouseList);
    }

    @PostMapping("/page")
    @ApiOperation("分页模糊查询库房信息")
    public ResultBean findByPage(@RequestBody WarehouseQuery warehouseQuery) {

        if(warehouseQuery == null
                || StringUtils.isEmpty(warehouseQuery.getPageNow())
                || StringUtils.isEmpty(warehouseQuery.getPageSize())
        ){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        PageBean<Warehouse> pageBean = warehouseService.findByPage(warehouseQuery);

        return ResultBean.success(pageBean);
    }

    @GetMapping("/address")
    @ApiOperation("查询现有全部分站的地址（去重）")
    public ResultBean getDistinctAddress(){
        List<String> distinctAddress = warehouseService.getDistinctAddress();

        return ResultBean.success(distinctAddress);
    }

}